package com.topone.controller;

import com.topone.Exception.FanZoneException;
import com.topone.Form.CouponForm;
import com.topone.Utils.KeyUntil;
import com.topone.dataObject.CouponCategory;
import com.topone.dataObject.TcCoupon;
import com.topone.service.CouponCategoryService;
import com.topone.service.TcCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/15,0015.
 */
@Controller
@Slf4j
@RequestMapping("/seller/coupon")
public class SellerCouponController {

    @Autowired
    private CouponCategoryService couponCategoryService;

    @Autowired
    private TcCouponService  couponService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "4") Integer size,
                             Map<String, Object> map){
        PageRequest request = new PageRequest(page - 1, size);
        Page<TcCoupon> couponPage = couponService.findAll(request);
        System.out.println(couponPage.getContent());
        map.put("couponPage", couponPage);
        map.put("currentPage", page);
        map.put("size", size);
//        orderDTOPage.getTotalPages()
        return new ModelAndView("coupon/list", map);

    }

    /**
     * 商品下架
     * @param couponId
     * @param map
     * @return
     */
    @GetMapping("/off_sale")
    public ModelAndView offSale(@RequestParam("couponId") String couponId,
                                Map<String,Object> map){

        try {
            couponService.offSale(couponId);
        }catch (FanZoneException e){
            map.put("msg",e.getMessage());
            map.put("url","/fanZone/seller/coupon/list");
            return  new ModelAndView("common/error",map);
        }
        map.put("url","/fanZone/seller/coupon/list");
        return new ModelAndView("common/success",map);
    }

    /**
     * 商品上架
     * @param couponId
     * @param map
     * @return
     */
    @GetMapping("/on_sale")
    public ModelAndView onSale(@RequestParam("couponId") String couponId,
                               Map<String,Object> map){
        try {
            couponService.onSale(couponId);
        }catch (FanZoneException e){

            map.put("msg",e.getMessage());
            map.put("url","/fanZone/seller/coupon/list");
            return  new ModelAndView("common/error",map);
        }
        map.put("url","/fanZone/seller/coupon/list");
        return new ModelAndView("common/success",map);
    }

    @GetMapping("/index")
    public  ModelAndView index(@RequestParam(value = "couponId", required =false) String couponId,
                               Map<String,Object> map){


        if(!StringUtils.isEmpty(couponId)){
            TcCoupon tcCoupon=couponService.findOne(couponId);
            map.put("tcCoupon",tcCoupon);
        }

        List<CouponCategory> categoryList=couponCategoryService.findAll();
        map.put("categoryList",categoryList);

        return new ModelAndView("coupon/index",map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid CouponForm couponForm,
                             BindingResult bindingResult,
                             Map<String,Object> map){
        TcCoupon tcCoupon=new TcCoupon();
        if(bindingResult.hasErrors()){
            log.error("【卖家端优惠券操作】 数据校验失败");
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/fanZone/seller/coupon/index");
            return new ModelAndView("/common/error",map);
        }
        if(!StringUtils.isEmpty(couponForm.getCouponId())){
            tcCoupon=couponService.findOne(couponForm.getCouponId());
        }else {
            couponForm.setCouponId(KeyUntil.getUniqueKey());
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BeanUtils.copyProperties(couponForm,tcCoupon);
        try {

            tcCoupon.setCouponPrice(new BigDecimal(couponForm.getCouponPrice()));
            tcCoupon.setUseDurationStart(simpleDateFormat.parse(couponForm.getUseDurationStart()));
            tcCoupon.setUseDurationEnd(simpleDateFormat.parse(couponForm.getUseDurationEnd()));
            tcCoupon.setActivityDurationEnd(simpleDateFormat.parse(couponForm.getActivityDurationStart()));
            tcCoupon.setActivityDurationEnd(simpleDateFormat.parse(couponForm.getActivityDurationEnd()));

        }catch (Exception e){
            log.error("【卖家端数据转换失败】");
            map.put("msg",e.getMessage());
            map.put("url","/fanZone/seller/coupon/index");
            return new ModelAndView("common/error",map);
        }

        try{
            couponService.save(tcCoupon);
        }catch (FanZoneException e){
            log.error("【卖家端保存优惠券失败】");
            map.put("msg",e.getMessage());
            map.put("url","/fanZone/seller/coupon/index");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/fanZone/seller/coupon/list");
        return new ModelAndView("common/success",map);
    }



}
